package cn.yunhe.biz.impl;

import cn.yunhe.biz.NewsBizDao;
import cn.yunhe.dao.NewsMapper;
import cn.yunhe.entity.News;
import cn.yunhe.entity.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component("newsBizDao")
public class NewsBizImpl implements NewsBizDao {
    @Autowired
    private NewsMapper newsMapper;
    @Override
    public Page searchNews(Page page) {
        int pageStart = (page.getPageIndex()-1)*page.getPageSize();
        page.setPageStart(pageStart);

        List<News> newsList = newsMapper.getNews(page);
        page.setResult(newsList);
        int counts = newsMapper.getCounts(page);

        counts=(counts/page.getPageSize())+(counts%page.getPageSize()==0?0:1);

        page.setTotalPages(counts);

        return page;
    }

    @Override
    public int delNewById(int newId) {
      return   newsMapper.delNewsById(newId);

    }

    @Override
    public Integer addNews(News news) {
        news.setBoardId(1);
        news.setUserId(1);
        return newsMapper.addNews(news);
    }

    @Override
    public News getNewsById(int newid) {
        return newsMapper.getNewsById(newid);
    }

    @Override
    public Integer updateNews(News news) {
        return newsMapper.updateNews(news);
    }

    public NewsMapper getNewsMapper() {
        return newsMapper;
    }

    public void setNewsMapper(NewsMapper newsMapper) {
        this.newsMapper = newsMapper;
    }
}
